package com.zjj.learn.javaplus.pkg07.demo702;


import java.io.*;
import java.net.Socket;

/**
 * 文件存储客户端，负责发送文件到服务端
 **/
public class UploadClient {

    private static String CLIENT_FILE_PATH = "file\\client\\";

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("127.0.0.1", 9699);
        //创建输入流，读取客户端文件
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(CLIENT_FILE_PATH + "image1.png"));

        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        System.out.println("开始传输");
        //写出数据
        byte[] buf = new byte[1024];
        int len;
        while ((len = bis.read(buf)) != -1) {
            //传输数据
            bos.write(buf, 0, len);
        }
        //关闭输出流，通知服务端，写出数据完毕
        socket.shutdownOutput();
        System.out.println("文件发送完毕");

        //获取服务端返回的数据
        InputStream is = socket.getInputStream();
        byte[] buf1 = new byte[1024 * 8];
        int i = is.read(buf1);
        System.out.println("服务端返回：" + new String(buf1, 0, i));
        //释放资源
        bis.close();
    }
}
